//
//  ReadPageController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/16.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "ReadPageController.h"
#import "ReadCollectionViewCell.h"
#import "ReadModel.h"
#import <MJRefresh/MJRefresh.h>
#import "UIImageView+WebCache.h"
#import "PWNetWorking.h"

@interface ReadPageController ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation ReadPageController
- (void)dealloc
{
    [_collectionView release];
    [_dataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatCollectionView];
    [self dataHandler];
}
- (void)creatCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 10 - 10) / 3, (self.view.frame.size.width - 10 - 10) / 3);
    NSLog(@"~~~~~%f~~~~", (self.view.frame.size.width - 10 - 10) / 3);
    // 行列间距
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    // 边界
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[ReadCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}
//- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    if(section == 0)
//    {
//        CGSize size = {self.view.frame.size.width - 10 - 10, 210};
//        return size;
//    } else {
//        CGSize size = {self.view.frame.size.width - 10 - 10, self.view.frame.size.width - 10 - 10};
//        return size;
//    }
//}

- (void)dataHandler
{
    NSString *str = @"http://api2.pianke.me/read/columns";
    //auth=C5VuFSS49v9DCsJzHMbslDJxQMqvZraIm325tCj1lcWP229VPSRVc7f4&client=1&deviceid=DB3A1424-3E92-41D9-8C86-10F84629D968&version=3.0.6
    NSDictionary *bodyDic = @{@"auth": @"C5VuFSS49v9DCsJzHMbslDJxQMqvZraIm325tCj1lcWP229VPSRVc7f4", @"client": @"1", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"version": @"3.0.6"};
    [PWNetWorking POSTWithURL:str body:bodyDic completion:^(id result) {
        self.dataArr = [ReadModel modelHanderWithArray:result[@"data"][@"list"]];
//        NSLog(@"%@", result);
        [_collectionView reloadData];
        
    }];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ReadModel *m = _dataArr[indexPath.item];
    [cell.coverImg sd_setImageWithURL:[NSURL URLWithString:m.coverimg]];
    cell.titleLabel.text = m.name;
    cell.eTitleLabel.text = m.enname;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
